package com.gamefx.engine1.display
{
	import com.gamefx.engine1.consts.ActionConst;
	import com.gamefx.engine1.consts.DirectionConst;
	import com.gamefx.resource.events.LoaderEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	[Event(name="parseComplete", type="com.gamefx.resource.events.LoaderEvent")]

	
	public class BitmapClip extends Bitmap
	{
		/**
		 * 碰撞检测时扩大检测范围区域
		 */
		private static const HIT_MOUSE_RANGE:int = 5;

		/**
		 * 鼠标位置碰撞检测矩形
		 */
		private static var hitRect:Rectangle = new Rectangle(0, 0, HIT_MOUSE_RANGE, HIT_MOUSE_RANGE);

		/**
		 * 鼠标位置矩形
		 */
		private static var mousePoint:Point = new Point();


		/**
		 * 数据对象
		 */
		public var data:BitmapClipData;

		/**
		 * 由于x,y是函数，因此在此记录，(循环一百万次：88:135)
		 */
		private var _x:int;
		private var _y:int;

		private var autoDraw:Boolean; //标识完成后是否自动绘制


		public function BitmapClip(autoDraw:Boolean = false)
		{
			this.autoDraw = autoDraw;
		}

		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------

		//----------------------------------
		//  currentAction
		//----------------------------------

		private var _currentAction:String;

		public function get currentAction():String
		{
			return _currentAction;
		}

		//----------------------------------
		//  currentDirection
		//----------------------------------

		private var _currentDirection:int;

		public function get currentDirection():int
		{
			return _currentDirection;
		}

		//----------------------------------
		//  currentFrame
		//----------------------------------

		private var _currentFrame:int = -1; //当前帧

		public function get currentFrame():int
		{
			return _currentFrame;
		}

		//----------------------------------
		//  currentFrame
		//----------------------------------

		public function get totalFrames():int
		{
			return data.getTotalFrames(_currentAction, _currentDirection);
		}

		
		//----------------------------------
		//  totalFrames
		//----------------------------------
		
		public function getTotalFrames(action:String, direction:int = DirectionConst.E):int
		{
			return data.getTotalFrames(action, direction);
		}



		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------

		/**
		 * 加载图像
		 * @param url 路径
		 * @param priority 加载优先级
		 * @param preload 是否预绘制
		 */
		public function load(url:String, priority:int = 1, preload:Boolean = false):void
		{
			if (data != null && data.url == url)
			{
				return;
			}
			
			var loader:BMFLoader = new BMFLoader(url, priority);
			loader.addEventListener(LoaderEvent.PARSE_COMPLETE, loader_parseCompleteHandler);
			loader.load();
		}

		/**
		 * 更新图片显示
		 * @param action 动作
		 * @param direction 方向
		 * @param frame 显示的帧
		 */
		public function draw(frame:int = 0, action:String = ActionConst.ACTION_MOTION, direction:int = DirectionConst.E):void
		{
			if (data == null || data.ready == false)
			{
				return;
			}

			if (_currentAction == action && _currentDirection == direction && _currentFrame == frame)
			{
				return;
			}
			
			_currentAction = action;
			_currentDirection = direction;
			_currentFrame = frame;
			
			bitmapData = data.getContent(action, direction, frame);

			//偏移
			if (bitmapData != null)
			{
				var bmfLabel:BMFLabel = data.labels.proxy[action + "#" + direction] as BMFLabel;
				var rect:* = bmfLabel.rects[frame];
				if (isNaN(rect) == false)
				{
					rect = bmfLabel.rects[rect];
				}
				var fixedX:Number = rect.x + bmfLabel.offsetX + data.offsetX;
				var fixedY:Number = rect.y + bmfLabel.offsetY + data.offsetY;
					
				if (_x != fixedX)
				{
					x = _x = fixedX;
				}
				
				if (_y != fixedY)
				{
					y = _y = fixedY;
				}
			}
		}

		/**
		 * 碰撞检测
		 * @return
		 */
		public function hitTest():Boolean
		{
			if (data == null || data.ready == false || !bitmapData)
			{
				return false;
			}

			hitRect.x = mouseX - HIT_MOUSE_RANGE;
			hitRect.y = mouseY - HIT_MOUSE_RANGE;

			return bitmapData.hitTest(mousePoint, 255, hitRect);
			return true;
		}
		
		
		private function loader_parseCompleteHandler(event:Event):void
		{
			var loader:BMFLoader = event.currentTarget as BMFLoader;
			loader.removeEventListener("parseComplete", loader_parseCompleteHandler);
			
			data = loader.data;
			
			if (autoDraw)
			{
				draw();
			}
			
			dispatchEvent(event);
		}

	}
}
